package com.peter.alertdialog;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialog;
import androidx.appcompat.app.AppCompatDialogFragment;

public class MyDialogActivity extends AppCompatActivity {
    private static final String TAG = "MyDialogActivity";

    MyDialog myDialog;

    MyDialogFragment myDialogFragment;


    void hello(MyDialog dialog) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_dialog);


        Button button = findViewById(R.id.button4);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                myDialogFragment = new MyDialogFragment();
                myDialogFragment.show(getSupportFragmentManager(), "show");
//                myDialog = new MyDialog(MyDialogActivity.this);
//                myDialog.show();
            }
        });
        //延时2秒主动关闭Activity
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                MyDialogActivity.this.finish();
            }
        }, 2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myDialog != null) {
            myDialog.cancel();
        }
        Log.e(TAG, "onDestroy");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop");
    }

    static class MyDialog extends AppCompatDialog {
        private String TAG = "Dialog";

        public MyDialog(@NonNull Context context) {
            super(context);
            setContentView(R.layout.dialog_demo);
        }

        @Override
        protected void onStop() {
            super.onStop();
            Log.e(TAG, "onStop");
        }

        @Override
        protected void onStart() {
            super.onStart();
            Log.e(TAG, "onStart");
        }

        @Override
        public void cancel() {
            super.cancel();
            Log.e(TAG, "cancel");
        }

        @Override
        public void dismiss() {
            super.dismiss();
            Log.e(TAG, "dismiss");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e(TAG, "onCreate");
        }


    }

    public static class MyDialogFragment extends AppCompatDialogFragment {
        private static final String TAG = "MyDialogFragment";

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "onCreate");
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Log.d(TAG, "onCreateView");
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        @Override
        public void onDetach() {
            super.onDetach();
            Log.d(TAG, "onDetach");
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d(TAG, "onDestroy");
        }
    }


}
